import type {
  AxiosInstance,
  AxiosRequestConfig
} from 'axios'
import type {
  Result
} from '@/utils/interface'
// post
export const postPromise = (service: AxiosInstance, url: string, params: any): Promise < Result > =>
  new Promise((resolve, reject) => {
    service
      .post(url, params)
      .then((response) => resolve(response.data))
      .catch((err) => {
        reject(err)
      })
  })

// get
export const getPromise = (service: AxiosInstance, url: string, params: any): Promise < Result > =>
  new Promise((resolve, reject) => {
    service
      .get(url, {
        params
      })
      .then((response) => resolve(response.data))
      .catch((err) => {
        reject(err)
      })
  })

// 导出 post
export const postExportPromise = (service: AxiosInstance, url: string, params: any): Promise < any > =>
  new Promise((resolve, reject) => {
    service({
        method: 'post',
        url: url,
        data: {
          ...params
        },
        responseType: 'blob'
      })
      .then((response) => {
        const res = response.data
        const filename = decodeURI(response.headers['content-disposition'].split(';')[1].split('filename=')[1])
        resolve({
          res,
          filename
        })
      })
      .catch((err) => {
        reject(err)
      })
  })

// 导出 get
export const exportPromise = (service: AxiosInstance, url: string, params: any): Promise < any > =>
  new Promise((resolve, reject) => {
    service({
        method: 'get',
        url: url,
        params,
        responseType: 'blob'
      })
      .then((response) => {
        const res = response.data
        try {
          const filename = decodeURI(response.headers['content-disposition'] ?.split(';')[1].split('filename=')[1])
          /*
            const unicodeStr = response.headers['content-disposition'].split(';')[2].split('fName=')[1]
            const filename = unescape(unicodeStr.replace(/\\u/g, '%u'))
              resolve({ res, filename })
          */
          let blob = new Blob([res])
          let downloadElement = document.createElement('a')
          let href = window.URL.createObjectURL(blob) //创建下载的链接
          downloadElement.href = href
          downloadElement.download = filename //下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() //点击下载
          document.body.removeChild(downloadElement) //下载完成移除元素
          window.URL.revokeObjectURL(href) //释放掉blob对象
          resolve({
            res,
            filename
          })
        } catch (error) {
          console.log(error)
          reject({
            res,
            filename: ''
          })
        }
      })
      .catch((err) => {
        reject(err)
      })
  })

// 上传
export const uploadPromise = (service: AxiosInstance, url: string, files: Blob, config ? : AxiosRequestConfig): Promise < any > =>
  new Promise((resolve, reject) => {
    config && (config.headers['Content-Type'] = 'multipart/form-data')

    service
      .post(url, files, config)
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        console.log(err, 'err')
        reject(err)
      })
  })